package filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class TopicFilter implements Filter {
    private String[] forbiddenwords;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String forbiddenword = filterConfig.getInitParameter("forbiddenword");
        forbiddenwords = forbiddenword.split(",");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        String content = servletRequest.getParameter("content");
        for (String forbiddenword : forbiddenwords) {
            if(content.contains(forbiddenword)){
                servletRequest.setAttribute("errorInfo","你输入的有禁用词");
                servletRequest.getRequestDispatcher("/topic.jsp").forward(servletRequest,servletResponse);
                return;
            }
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
